package cn.edu.scau.cmi.ema.controller.rest;


import cn.edu.scau.cmi.ema.domain.NewUser;
import cn.edu.scau.cmi.ema.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
//web-starter自动加载controller???

@RestController
public class TestUserController {
    @Autowired
    UserRepository userRepository;

    @RequestMapping(value ="/register", method = RequestMethod.GET)
    public String register(Model model, NewUser newUser) {
        model.addAttribute("user", newUser);
        return "register";
    }

    @RequestMapping(value ="/addUser")
    public String addUser(Model model, NewUser newUser) {
        model.addAttribute("user", newUser);
        NewUser isNewNewUser = userRepository.findUserByName(newUser.getName());
        //判断该用户名是否被注册过
        if (null == isNewNewUser) {
            userRepository.save(newUser);
            return "redirect:register?success";
        } else {
            return "redirect:register?error";
        }
    }
}
